#include "ConvexHullAlgorithm.h"

ConvexHullAlgorithm::ConvexHullAlgorithm( const std::vector<clan::Vec2d>& points )
	: m_points(points),
	m_convexHullLines(),
	m_convexHull(nullptr, nullptr)
{
}

void ConvexHullAlgorithm::Run( void )
{
	m_convexHull = GetConvexHull(0, m_points.size() - 1);
}

std::vector<clan::Line2d> ConvexHullAlgorithm::GetConvexHullLines( void )
{
	m_convexHullLines = m_convexHull.GetLines();
	return m_convexHullLines;
}

ConvexHull ConvexHullAlgorithm::GetConvexHull( int startIndex, int endIndex ) const
{
	if(ConvexHull::IsBruteForceComputable(startIndex, endIndex))
	{
		return ConvexHull(m_points, startIndex, endIndex);
	}
	else
	{
		ConvexHull convexHullLeft = GetConvexHull(startIndex, static_cast<int>(floor((startIndex + endIndex) / 2.)));
		ConvexHull convexHullRight = GetConvexHull(static_cast<int>(floor((startIndex + endIndex) / 2.)) + 1, endIndex);
		return convexHullLeft.MergeRight(convexHullRight);
	}
}
